const responseData = require('../responseData')

const tokens = ['admin-token']

const userInfo = {
	roles: ['admin'],
	introduction: 'I am a super administrator',
	avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
	name: 'Super Admin',
	token: 'admin-token'
}

module.exports = [
	// user login
	{
		url: 'user/login',
		type: 'post',
		response: config => {
			const { username, password } = config.body
			const token = config.headers.access_token || ''
			console.log(token)
			if (
				tokens.findIndex(itemToken => itemToken === token) > -1 ||
				(username === 'admin' && password === '123456')
			) {
				return responseData(userInfo)
			}
			return {
				err_code: 60204,
				err_msg: '帐号或密码不正确。'
			}
		}
	},

	// get user info
	{
		url: 'user/info',
		type: 'post',
		response: config => {
			const token = config.headers.access_token || ''
			// mock error
			if (tokens.findIndex(itemToken => itemToken === token) === -1) {
				return {
					err_code: 50008,
					err_msg: '登录失败，无法获得用户详细信息'
				}
			}

			return responseData(userInfo)
		}
	},

	// user logout
	{
		url: 'user/logout',
		type: 'post',
		response: _ => {
			return responseData(userInfo)
		}
	}
]
